package com.tjetc.service.Impl;

import com.tjetc.dao.ShoppingCartMapper;
import com.tjetc.entity.ShoppingCart;
import com.tjetc.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    /**
     * 添加购物车
     * @param shoppingCart 购物车对象
     * @return
     */
    @Override
    public int add(ShoppingCart shoppingCart) {
        shoppingCartMapper.add(shoppingCart);
        return shoppingCart.getId();
    }

    /**
     * 删除指定id的购物车商品元素
     * @param id 商品id
     * @return 返回是否删除完成
     */
    @Override
    public int delete(Integer id) {
        int i = shoppingCartMapper.delete(id);
        return i;
    }

    /**
     * 根据id查询购物车商品信息
     * @param id 传入的购物车商品元素id
     * @return 返回查询到的ShoppingCart类型
     */
    @Override
    public ShoppingCart select(Integer id) {
        ShoppingCart shoppingCart = shoppingCartMapper.select(id);
        return shoppingCart;
    }

    /**
     * 更新指定id的increment
     * @param id 指定的元素id
     * @param increment 需要更新成的值
     * @return 返回是否更新成功的int类型
     */
    @Override
    public int updateNumber(Integer id, Integer increment) {
        LocalDateTime now = LocalDateTime.now();
        return shoppingCartMapper.updateNumber(id, increment, now);
    }

    /**
     * 查询当前用户在当前商家的所有购物车商品信息
     * @param userId 指定用户id
     * @param merchantId 指定商家id
     * @return 返回购物车商品元素列表
     */
    @Override
    public List<ShoppingCart> selectAll(Integer userId, Integer merchantId) {
        List<ShoppingCart> shoppingCarts = shoppingCartMapper.selectAll(userId, merchantId);
        return shoppingCarts;
    }

    /**
     * 删除当前用户在当前商家中购物车中所有元素
     * @param userId 指定用户id
     * @param merchantId 指定商家id
     * @return 返回是否删除成功
     */
    @Override
    public int deleteAll(Integer userId, Integer merchantId) {
        return shoppingCartMapper.deleteAll(userId, merchantId);
    }

    /**
     * 在当前用户下和当前商家中，将指定商品添加到购物车中，并且表明数量
     * @param userId 指定用户id
     * @param dishId 指定菜品id
     * @param merchantId 指定商家id
     * @param increment 数量
     * @return 返回是否更新成功
     */
    @Transactional
    @Override
    public int insertByShoppingCart(Integer userId, Integer dishId, Integer merchantId, Integer increment) {
        ShoppingCart shoppingCart1 = shoppingCartMapper.selectById(userId, dishId, merchantId);
        if (shoppingCart1 != null) {
            int rows = shoppingCartMapper.updateNumber(shoppingCart1.getId(), increment, LocalDateTime.now());
            if (rows <= 0) {
                throw new RuntimeException("购物车更新菜品数量错误");
            }
            return shoppingCart1.getId();
        } else {
            ShoppingCart shoppingCart2 = new ShoppingCart();
            shoppingCart2.setCreateTime(LocalDateTime.now());
            shoppingCart2.setUpdateTime(LocalDateTime.now());
            shoppingCart2.setDishId(dishId);
            shoppingCart2.setUserId(userId);
            shoppingCart2.setNumber(increment);
            shoppingCart2.setMerchantId(merchantId);
            shoppingCartMapper.add(shoppingCart2);
            return shoppingCart2.getId();
        }
    }
}
